package com.bruce.asurada.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bruce.asurada.common.Result;
import com.bruce.asurada.common.dto.*;
import com.bruce.asurada.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @ResponseBody
    public Result<Void> register(@RequestBody UserRegisterDto userRegisterDto) {
        logger.info("用户注册请求: {}", userRegisterDto);
        try{
           boolean flag =  userService.register(userRegisterDto);
           return flag ? Result.success() : Result.error("用户注册失败");
        }catch (Exception e){
            logger.error("用户注册过程发生错误: {}", e.getMessage(), e);
            return Result.error("用户注册失败，请稍后重试");
        }
    }

    /**
     * 获取用户信息
     * @param queryDTO
     * @return
     */
    @PostMapping("/info")
    public Result<UserInfoDto> getUserInfo(@RequestBody UserQueryDTO queryDTO){
        try{
            UserInfoDto userInfoDto = userService.getUserInfo(queryDTO);
            return Result.success(userInfoDto);
        }catch (Exception e){
            logger.error("用户信息获取过程中发生错误: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
